package com.learn.springbootnetty.tcpdemo.线程池版;


import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class MyRunnable implements Runnable {
    private Socket socket;
    public Socket getSocket() {
        return socket;
    }
    public void setSocket(Socket socket) {
        this.socket = socket;
    }
    public MyRunnable(Socket socket) {
        this.socket = socket;
    }
    @Override
    public void run() {
        try {
            // 3.读取数据并保存到本地文件中
            BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
            socket.getPort();
            String name = UUID.randomUUID().toString().replace("-","");
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\gsafety\\适岗状态课题\\课题5\\营运车船驾驶人员适岗状态网联智能监测预警平台-0611\\专题图管理"+socket.getPort()+"2.png"));
            int len;
            byte[] bytes = new byte[1024];
            while((len = bis.read(bytes)) != -1){
                bos.write(bytes,0,len);
            }
            // 4.回写数据
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("上传成功!");
            bw.newLine(); // 换行
            bw.flush(); // 刷新流
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            // 5.释放资源
            // 关闭连接通道,断开跟客户端之间的连接
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}